package com.example.ui_view.nework;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.example.ui_view.R;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class ProgressImageActivity extends AppCompatActivity {
    ImageView iv_image;
    ProgressBar pb_loding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_image);
        iv_image = findViewById(R.id.iv_image);
        pb_loding = findViewById(R.id.pb_loding);
    }

    public void start(View view) {
        String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1608011852801&di=5ac0bab1806ff76596a2a8d9b8284659&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F71%2F56%2F16300000988660128426569668958.jpg";
        iv_image.setImageBitmap(null);
        new ProgressImageActivity.ImageAsyncTack().execute(url);

    }
    class ImageAsyncTack extends AsyncTask<String,Integer, Bitmap> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pb_loding.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            iv_image.setImageBitmap(bitmap);
            pb_loding.setVisibility(View.GONE);
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            pb_loding.setProgress(values[0]);
        }

        @Override
        protected Bitmap doInBackground(String... strings) {
            URL url = null;
            HttpURLConnection connection = null;
            InputStream is = null;
            Bitmap bitmap = null;
            OutputStream os = null;
            try {
                url= new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1608011852801&di=5ac0bab1806ff76596a2a8d9b8284659&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F71%2F56%2F16300000988660128426569668958.jpg");
                connection = (HttpURLConnection) url.openConnection();
                is = connection.getInputStream();
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                os =new BufferedOutputStream(bos);
                int total = connection.getContentLength();
                int current = 0;
                int read = 0;
                byte[] data = new byte[1024];
                while ((read = is.read(data)) != -1){
                    os.write(data,0,read);
                    current = current+read;
                    int progress = (int) ((float)current/total*100);
                    Thread.sleep((long) (int)(Math.random()*3));
                    publishProgress(progress);
                }
                os.flush();
                byte[] bitmapData = bos.toByteArray();
                bitmap = BitmapFactory.decodeByteArray(bitmapData,0,bitmapData.length);

            }catch (Exception e){
                e.printStackTrace();
            }
            return bitmap;
        }
    }
}
